#include<unistd.h>

#include<stdio.h>
#include<string.h>


// void TestWrite()
// {
    
//     const char* filename = "bite";
//     FILE* pf = fopen(filename, "w");

//     const char* mess = "linux so easy!";

//     fwrite(mess, 1, strlen(mess), pf);     //向pf文件中写入mess这条数据
//     fclose(pf);

// }

// void TestRead()
// {
//     const char* filename = "bite";
//     FILE* pf = fopen(filename, "r");   //读方式打开
//     char buffer[128];
//     int n = fread(buffer, 1, sizeof(buffer) - 1, pf); //从文件里面读数据
//     if (n > 0) buffer[n] = 0;
//     else printf("读取失败");

//     printf("%s\n", buffer);
//     fclose(pf);
// }

// void TestSeek()
// {
    
//     const char* filename = "bite";
//     FILE* pf = fopen(filename, "r");   //读方式打开
//     fseek(pf, 1, SEEK_SET);
//     char buffer[128];
//     int n = fread(buffer, 1, sizeof(buffer) - 1, pf); //从文件里面读数据
//     if (n > 0) buffer[n] = 0;
//     else printf("读取失败");

//     printf("%s\n", buffer);   //不打印第一个字符
//     fclose(pf);

// }



// int main()
// {
//     TestWrite();

//     TestRead();

//     TestSeek();

//     return 0;
// }
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>


void TestWrite()
{
    
    const char* filename = "bite";
    int fd = open(filename, O_WRONLY);  //之以写方式打开

    const char* mess = "linux so easy!";

    write(fd, mess, strlen(mess));     //向pf文件中写入mess这条数据
    close(fd);

}

void TestRead()
{
    const char* filename = "bite";
    int fd = open(filename, O_RDONLY);   //读方式打开
    char buffer[128];
    int n = read(fd, buffer, sizeof(buffer) - 1); //从文件里面读数据
    if (n > 0) buffer[n] = 0;
    else printf("读取失败");

    printf("%s\n", buffer);
    close(fd);
}

void TestSeek()
{
    
    const char* filename = "bite";
    int fd = open(filename, O_RDONLY);   //读方式打开
    lseek(fd, 1, SEEK_SET);
    char buffer[128];
    int n = read(fd, buffer, sizeof(buffer) - 1); //从文件里面读数据
    if (n > 0) buffer[n] = 0;
    else printf("读取失败");

    printf("%s\n", buffer);   //不打印第一个字符
    close(fd);

}



int main()
{
    TestWrite();

    TestRead();

    TestSeek();

    return 0;
}

